//
//  MenuViewController.m
//  DeliciousFood1
//
//  Created by brother on 15/10/20.
//  Copyright (c) 2015年 com.hxm. All rights reserved.
//

#import "MenuViewController.h"
#import "ZBFlowView.h"
#import "ZBWaterView.h"
#define key @"67d58a3e10984fa8abdef544c49096ce"
#import <AFNetworking.h>
#import "ShowTableViewController.h"


@interface TestData : NSObject

//@property (nonatomic,strong) UIColor *color;
@property (nonatomic, strong)UIImage *image;
@property (nonatomic,assign) CGFloat height;

@end

@implementation TestData

@end

@interface MenuViewController ()<ZBWaterViewDatasource,ZBWaterViewDelegate>
{
    NSMutableArray *_testDataArr;
    ZBWaterView *_waterView;
}
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSMutableArray *mu;
@property (nonatomic,assign)int count;
@property (weak, nonatomic) IBOutlet UINavigationItem *menuTitle;
- (IBAction)back:(id)sender;
@end

@implementation MenuViewController

//懒加载
- (NSMutableArray *)mu
{
    if (_mu == nil) {
        _mu = [NSMutableArray array];
    }
    return  _mu;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.count = 1;
    [self requestData];
    self.menuTitle.title = self.type;
    // Do any additional setup after loading the view, typically from a nib.
    _waterView = [[ZBWaterView alloc]  initWithFrame:CGRectMake(0, 30, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    _waterView.waterDataSource = self;
    _waterView.waterDelegate = self;
    _waterView.isDataEnd = NO;
    [self.view addSubview:_waterView];
    
    //延迟加载数据
    [self performSelector:@selector(getData) withObject:nil afterDelay:20.0f];
}

//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *url = @"http://apis.haoservice.com/lifeservice/cook/query";
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"key"] = key;
    dic[@"menu"] = self.type;
    dic[@"pn"] = [NSNumber numberWithInt:self.count];
    dic[@"rn"] = @8;
    
    [manager GET:url parameters:dic success:^void(AFHTTPRequestOperation * operation, id  responseObject) {
        NSArray *array = responseObject[@"result"];
        self.array = array;
        [self.mu addObjectsFromArray:self.array];
//        NSDictionary *dic = self.mu[7];
        NSLog(@"%ld",self.mu.count);
    } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
        NSLog(@"%@",error);
    }];
}

//延迟加载数据
- (void)getData
{
    _testDataArr = [NSMutableArray array];
    for (int i=0; i<8; i++) {
        TestData *data = [[TestData alloc] init];
        NSDictionary *dic = self.array[i];
        NSLog(@"%@",dic[@"title"]);
        NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"albums"]]];
//        NSLog(@"%@",dic[@"albums"]);
        data.image = [UIImage imageWithData:data1];
        data.height = arc4random()%100;
        data.height = 200 + data.height;
        [_testDataArr addObject:data];
    }
    
    [_waterView reloadData];
}


- (void)show{
    NSLog(@"%@",self.array);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ZBWaterViewDatasource
- (NSInteger)numberOfFlowViewInWaterView:(ZBWaterView *)waterView
{
    return [_testDataArr count];
}

- (CustomWaterInfo *)infoOfWaterView:(ZBWaterView*)waterView
{
    CustomWaterInfo *info = [[CustomWaterInfo alloc] init];
    info.topMargin = 0;
    info.leftMargin = 10;
    info.bottomMargin = 0;
    info.rightMargin = 10;
    info.horizonPadding = 5;
    info.veticalPadding = 5;
    info.numOfColumn = 2;
    return info;
}

- (ZBFlowView *)waterView:(ZBWaterView *)waterView flowViewAtIndex:(NSInteger)index
{
    TestData *data = [_testDataArr objectAtIndex:index];
    ZBFlowView *flowView = [waterView dequeueReusableCellWithIdentifier:@"cell"];
    if (flowView == nil) {
        flowView = [[ZBFlowView alloc] initWithFrame:CGRectZero];
        flowView.reuseIdentifier = @"cell";
    }
    flowView.index = index;
    //    flowView.backgroundColor = data.color;
    [flowView.button setBackgroundImage:data.image forState:UIControlStateNormal];
    
    return flowView;
}

- (CGFloat)waterView:(ZBWaterView *)waterView heightOfFlowViewAtIndex:(NSInteger)index
{
    TestData *data = [_testDataArr objectAtIndex:index];
    return data.height;
}


#pragma mark - ZBWaterViewDelegate
- (void)needLoadMoreByWaterView:(ZBWaterView *)waterView;
{
    self.count += 1;
    [self requestData];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [NSThread sleepForTimeInterval:2.0];
        for (int i=0; i<8; i++) {
            TestData *data = [[TestData alloc] init];
            NSDictionary *dic = self.array[i];
            NSLog(@"%@",dic[@"title"]);
            NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"albums"]]];
            data.image = [UIImage imageWithData:data1];
            data.height = arc4random()%100;
            data.height = 200 + data.height;
            [_testDataArr addObject:data];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_waterView endLoadMore];
            [_waterView reloadData];
        });
    });
}

- (void)phoneWaterViewDidScroll:(ZBWaterView *)waterView
{
    //do what you want to do
    return;
}

- (void)waterView:(ZBWaterView *)waterView didSelectAtIndex:(NSInteger)index
{
    NSLog(@"didSelectAtIndex%ld",(long)index);
    NSDictionary *dic = self.mu[index];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ShowTableViewController *show = [storyBoard instantiateViewControllerWithIdentifier:@"show"];
    show.dic = dic;
    [self presentViewController:show animated:YES completion:nil];
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
